package com.erp.database.utils;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

	/**
	 * dateStr2Long
	 * @param date
	 * @return
	 */
	public static Long dateStr2Long(String date,String pattern){
		if(date==null || "".equals(date))return null;
		if(pattern==null){
			pattern="yyyy-MM-dd";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			try {
				Date d=sdf.parse(date);
				Long l=d.getTime();
				return l;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		return null;
	}
	
	/**
	 * long2Str
	 * @param d
	 * @return
	 */
	public static String string2Date(Long  d){
		if(d==null)return null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s=sdf.format(new Date(d));
		return s;
	}
	
	
	public static String date2String(Date date){
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");//格式化日期显示方式
		String sdate = sf.format(date);
		return sdate;
	}

	public static String date2TimeString(Date date){
		if(date==null)return null;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化日期显示方式
		String sdate = sf.format(date);
		return sdate;
	}
	
	public static Date string2Date(String dateTemp){
		if(dateTemp==null || "".equals(dateTemp))return null;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");//格式化日期显示方式
		String year = dateTemp.substring(0, 4);
        String month = dateTemp.substring(5, 7);
        String day = dateTemp.substring(8, 10);
        String sdate = year+"-"+month+"-"+day;
        Date date = null;
		try {
			date = sf.parse(sdate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
		
	}


	public static String string2DateTime(String dateTemp){
		if(dateTemp==null || "".equals(dateTemp))return null;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//格式化日期显示方式
		String year = dateTemp.substring(0, 4);
		String month = dateTemp.substring(5, 7);
		String day = dateTemp.substring(8, 10);
		String h=dateTemp.substring(11,13);
		String m=dateTemp.substring(14,16);
		String s=dateTemp.substring(17,19);
		String sdate = year+"-"+month+"-"+day+" "+h+":"+m+":"+s;
		Date date = null;
		try {
			date = sf.parse(sdate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return sdate;

	}
	
	public static String DateTime2StringTime(Date dateTime){
		if(CommonUtil.isEmpty(dateTime))return null;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm");//格式化日期显示方式
		String sdate = sf.format(dateTime);
		return sdate;
	}
	
	public static String time2String (Date timelong){

	Long  t=timelong.getTime();
	
	SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH:mm:ss");
	
	String time=Time.valueOf(t.toString()).toString();
	
	return time ;
	

	
}
	
	public static Date str2DateTime(String dateTemp){
		if(dateTemp==null || "".equals(dateTemp))return null;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm");//格式化日期显示方式
/*		String year = dateTemp.substring(0, 4);
		String month = dateTemp.substring(5, 7);
		String day = dateTemp.substring(8, 10);
		String h=dateTemp.substring(11,13);
		String m=dateTemp.substring(14,16);
		String s=dateTemp.substring(17,19);
		String sdate = year+"-"+month+"-"+day+" "+h+":"+m;+":"+s;*/
		Date date = null;
		try {
			date = sf.parse(dateTemp);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;

	}

	public static String strAddMonth(String startDate, int i) {
		String year = startDate.substring(0, 4);
        String month = startDate.substring(5, 7);
        String day = startDate.substring(8, 10);
	    Calendar calendar = new GregorianCalendar(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day));
	    calendar.add(calendar.MONTH,i);//把日期往后增加一天.整数往后推,负数往前移动 
	    Date date=calendar.getTime();
		return date2String(date);
	}
	

}
